package com.qc31.monitorlib.ui.listmonitor

import android.text.Html
import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qc31.baselib.custom.LeftRightTextView
import com.qc31.baselib.utils.Language
import com.qc31.baselib.utils.loadStringDImgError
import com.qc31.baselib.utils.stateColor
import com.qc31.comlib.entity.GpsDetail
import com.qc31.monitorlib.R

/** @Author Jasper @Time 2020/11/12 @Desc 列表监控适配器 */
class ListMonitorAdapter :
    BaseQuickAdapter<GpsDetail, BaseViewHolder>(R.layout.item_moitor_list) {
    private val isChinese =
        Language.instance.language == "zh_CN" || Language.instance.language == "zh_TW"
    var isPlate = true

    override fun convert(holder: BaseViewHolder, item: GpsDetail) {
        item.run {
            val popup = holder.getView<ImageView>(R.id.iv_list_popup)
            val carMap = holder.getView<ImageView>(R.id.iv_list_car_monitor)
            popup.setOnClickListener { mCallback?.callBack(item) }
            carMap.setOnClickListener { mCallback?.mapCallbacks(item) }

            holder.getView<ImageView>(R.id.iv_list_popup).setOnClickListener {
                mCallback?.callBack(item)
            }
            holder.getView<ImageView>(R.id.iv_list_logo_car)
                .loadStringDImgError(context, iconLink, R.mipmap.ic_car_location_no)
            holder.setText(R.id.tv_list_name_plate, if (isPlate) carPlate else carName)
                .setText(R.id.tv_address, addr ?: "")
            val lrTvTime = holder.getView<LeftRightTextView>(R.id.lr_tv_list_time)
            val lrTvState = holder.getView<LeftRightTextView>(R.id.lr_tv_list_state)
            val lrTvSpeed = holder.getView<LeftRightTextView>(R.id.lr_tv_list_speed)
            val lrTvDriver = holder.getView<LeftRightTextView>(R.id.lr_tv_list_driver)
            val lrTvMile = holder.getView<LeftRightTextView>(R.id.lr_tv_list_mile)
            val lrTvRunStop = holder.getView<LeftRightTextView>(R.id.lr_tv_list_run_time)
            if (ListMonitorModel.instance.getTimeShow()) {
                lrTvTime.visibility = View.VISIBLE
                lrTvTime.setRightText(time)
            } else {
                lrTvTime.visibility = View.GONE
            }
            if (!isChinese)
                lrTvState.getRightTv().textSize = 12f
            if (ListMonitorModel.instance.getStateShow()) {
                lrTvState.visibility = View.VISIBLE
                val string =
                    "<font color=${stateColor(state)} >$stateCn</font><font color='#666666'> | ACC$accTime</font>"
                lrTvState.setRightText(Html.fromHtml(string))
            } else {
                lrTvState.visibility = View.GONE
            }
            if (ListMonitorModel.instance.getSpeedShow()) {
                lrTvSpeed.visibility = View.VISIBLE
                lrTvSpeed.setRightText("$speed${context.getString(R.string.unit_speed)}")
            } else {
                lrTvSpeed.visibility = View.GONE
            }
            if (ListMonitorModel.instance.getDriverShow()) {
                lrTvDriver.visibility = View.VISIBLE
                lrTvDriver.setRightText(if (drvName.isEmpty()) context.getString(R.string.desc_no_about_message) else drvName)
            } else {
                lrTvDriver.visibility = View.GONE
            }
            if (ListMonitorModel.instance.getMileShow()) {
                lrTvMile.visibility = View.VISIBLE
                lrTvMile.setRightText("$preMile / $mile ${context.getString(R.string.unit_mile)}")
            } else {
                lrTvMile.visibility = View.GONE
            }
            if (ListMonitorModel.instance.getRunStopShow()) {
                lrTvRunStop.visibility = View.VISIBLE
                lrTvRunStop.setRightText(runStopTime)
            } else {
                lrTvRunStop.visibility = View.GONE
            }
        }
    }

    private var mCallback: ListMonitorCallback? = null
    fun setCallBack(mCallback: ListMonitorCallback) {
        this.mCallback = mCallback
    }

    interface ListMonitorCallback {
        fun callBack(item: GpsDetail)
        fun mapCallbacks(item: GpsDetail)
    }
}